<script setup>
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import { ref, reactive, inject } from 'vue'

// 注入全局状态
const store = inject('$store')

// 应用生命周期
onLaunch(() => {
  console.log('App Launch')
  // 初始化应用
  initApp()
})

onShow(() => {
  console.log('App Show')
  // 检查网络状态
  checkNetworkStatus()
})

onHide(() => {
  console.log('App Hide')
})

// 初始化应用
const initApp = () => {
  // 从本地恢复登录状态等
  if (store && store.actions && typeof store.actions.initializeState === 'function') {
    store.actions.initializeState()
  }
  // 检查登录状态
  checkLoginStatus()
  // 检查网络状态
  checkNetworkStatus()
  // 监听网络状态变化
  listenNetworkChange()
}

// 检查登录状态
const checkLoginStatus = () => {
  if (store.getters.isAuthenticated.value) {
    // 已登录，检查token有效性
    // 这里可以调用API验证token
    console.log('用户已登录')
  } else {
    console.log('用户未登录')
  }
}

// 检查网络状态
const checkNetworkStatus = () => {
  uni.getNetworkType({
    success: (res) => {
      const isConnected = res.networkType !== 'none'
      store.actions.setNetworkStatus(isConnected)
      
      if (!isConnected) {
        uni.showToast({
          title: '网络连接失败，请检查网络设置',
          icon: 'none',
          duration: 3000
        })
      }
    }
  })
}

// 监听网络状态变化
const listenNetworkChange = () => {
  uni.onNetworkStatusChange((res) => {
    store.actions.setNetworkStatus(res.isConnected)
    
    if (res.isConnected) {
      uni.showToast({
        title: '网络已连接',
        icon: 'success',
        duration: 1500
      })
    } else {
      uni.showToast({
        title: '网络连接失败',
        icon: 'none',
        duration: 2000
      })
    }
  })
}
</script>

<style lang="scss">
/*每个页面公共css */
@import './static/css/global.css';

/* 全局变量 */
:root {
  --primary-color: #0079FE;
  --success-color: #07C160;
  --warning-color: #FF8C00;
  --error-color: #FA5151;
  --text-color: #333333;
  --text-secondary: #666666;
  --text-placeholder: #999999;
  --border-color: #E5E5E5;
  --background-color: #F8F8F8;
  --white: #FFFFFF;
}

/* 基础样式重置 */
* {
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-color);
  background-color: var(--background-color);
}

/* 页面基础样式 */
.page {
  min-height: 100vh;
  background-color: var(--background-color);
}

/* 导航栏样式 */
.custom-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: var(--white);
  border-bottom: 1px solid var(--border-color);
  z-index: 999;
  
  .navbar-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 44px;
    padding: 0 15px;
    
    .navbar-title {
      font-size: 18px;
      font-weight: 600;
      color: var(--text-color);
    }
    
    .navbar-btn {
      width: 32px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 16px;
      background-color: rgba(0, 0, 0, 0.05);
    }
  }
}

/* 内容区域 */
.main-content {
  padding-top: 44px;
  min-height: calc(100vh - 44px);
}

/* 底部安全区域 */
.safe-area-bottom {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

/* 通用按钮样式 */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.2s;
  
  &.btn-primary {
    background-color: var(--primary-color);
    color: var(--white);
    
    &:active {
      background-color: #0066CC;
    }
  }
  
  &.btn-secondary {
    background-color: var(--white);
    color: var(--primary-color);
    border: 1px solid var(--primary-color);
    
    &:active {
      background-color: #F5F5F5;
    }
  }
  
  &.btn-large {
    height: 48px;
    padding: 0 24px;
  }
  
  &.btn-medium {
    height: 36px;
    padding: 0 16px;
  }
  
  &.btn-small {
    height: 28px;
    padding: 0 12px;
    font-size: 14px;
  }
}

/* 通用输入框样式 */
.input {
  height: 44px;
  padding: 0 12px;
  background-color: var(--white);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-size: 16px;
  color: var(--text-color);
  
  &::placeholder {
    color: var(--text-placeholder);
  }
  
  &:focus {
    border-color: var(--primary-color);
  }
}

/* 卡片样式 */
.card {
  background-color: var(--white);
  border-radius: 12px;
  overflow: hidden;
  
  &.card-shadow {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
}

/* 列表项样式 */
.list-item {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  background-color: var(--white);
  border-bottom: 1px solid var(--border-color);
  
  &:last-child {
    border-bottom: none;
  }
  
  &:active {
    background-color: #F5F5F5;
  }
}

/* 加载状态 */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: var(--text-secondary);
}

/* 空状态 */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: var(--text-secondary);
  
  .empty-icon {
    width: 80px;
    height: 80px;
    margin-bottom: 12px;
    opacity: 0.5;
  }
  
  .empty-text {
    font-size: 16px;
  }
}

/* 徽章样式 */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  background-color: var(--error-color);
  color: var(--white);
  font-size: 12px;
  border-radius: 9px;
  
  &.badge-dot {
    width: 8px;
    height: 8px;
    min-width: 8px;
    padding: 0;
    border-radius: 4px;
  }
}

/* 分割线 */
.divider {
  height: 1px;
  background-color: var(--border-color);
  margin: 8px 0;
}

/* 工具类 */
.flex {
  display: flex;
}

.flex-1 {
  flex: 1;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.text-center {
  text-align: center;
}

.text-right {
  text-align: right;
}

.text-primary {
  color: var(--primary-color);
}

.text-secondary {
  color: var(--text-secondary);
}

.text-placeholder {
  color: var(--text-placeholder);
}

.text-error {
  color: var(--error-color);
}

.bg-white {
  background-color: var(--white);
}

.bg-gray {
  background-color: var(--background-color);
}

.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 16px; }
.mt-20 { margin-top: 20px; }

.mb-8 { margin-bottom: 8px; }
.mb-12 { margin-bottom: 12px; }
.mb-16 { margin-bottom: 16px; }
.mb-20 { margin-bottom: 20px; }

.ml-8 { margin-left: 8px; }
.ml-12 { margin-left: 12px; }
.ml-16 { margin-left: 16px; }

.mr-8 { margin-right: 8px; }
.mr-12 { margin-right: 12px; }
.mr-16 { margin-right: 16px; }

.p-8 { padding: 8px; }
.p-12 { padding: 12px; }
.p-16 { padding: 16px; }
.p-20 { padding: 20px; }

.px-8 { padding-left: 8px; padding-right: 8px; }
.px-12 { padding-left: 12px; padding-right: 12px; }
.px-16 { padding-left: 16px; padding-right: 16px; }

.py-8 { padding-top: 8px; padding-bottom: 8px; }
.py-12 { padding-top: 12px; padding-bottom: 12px; }
.py-16 { padding-top: 16px; padding-bottom: 16px; }
</style>